#include "sessiondetailwidget.h"

#include "Debug.h"

AvatarItem::AvatarItem(const QIcon &avatar, const QString &name):
    m_avatarBtn(nullptr), m_nameLabel(nullptr)
{
    setFixedSize(50, 70);
    setCursor(Qt::PointingHandCursor);
    setStyleSheet("QWidget { border-radius: 5px; }"
                  "QWidget:hover { background-color: rgb(235, 235, 235); }"
                  "QLabel { background-color:transparent; }");

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 10, 0, 10);
    setLayout(layout);
    layout->setAlignment(Qt::AlignCenter);

    m_avatarBtn = new QPushButton();
    m_avatarBtn->setIcon(avatar);
    m_avatarBtn->setFixedSize(32, 32);
    m_avatarBtn->setIconSize(QSize(32, 32));
    m_avatarBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; }"
                               "QPushButton:pressed { background-color: rgb(235, 235, 235); }");

    m_nameLabel = new QLabel(name);
    m_nameLabel->setAlignment(Qt::AlignCenter);

    int MaxWidth = 37;  // 最大文字宽度
    QFontMetrics metrics(m_nameLabel->font());
    int totalWidth = metrics.horizontalAdvance(name);  // 实际文字长度
    if(totalWidth > MaxWidth){  // 如果过长，就截取一部分+省略号，具体算法可以画图便于理解
        QString tail = "...";
        int tailWidth = metrics.horizontalAdvance(tail);
        int availableSize = MaxWidth - tailWidth;
        QString truncatedName = name.left(name.length() * availableSize / totalWidth);
        m_nameLabel->setText(truncatedName + tail);
    }

    layout->addWidget(m_avatarBtn);
    layout->addWidget(m_nameLabel);
}

QPushButton *AvatarItem::getAvatarBtn()
{
    return m_avatarBtn;
}

SessionDetailWidget::SessionDetailWidget(const model::UserInfo& userinfo, QWidget* parent):QDialog(parent) {
    this->setWindowTitle("会话详情");
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(200, 200);

    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(0);
    setLayout(layout);

    QIcon cross(":/resource/image/cross.png");
    AvatarItem *addItem = new AvatarItem(cross, "添加");
    layout->addWidget(addItem, 0, 0);

#if TEST_UI

    QIcon avatar(":/resource/image/defaultAvatar2.jpg");
    AvatarItem *avatarItem = new AvatarItem(avatar, "zhangsan213123123");
    layout->addWidget(avatarItem, 0, 1);

#endif
    // 添加自己
    AvatarItem *currentUser = new AvatarItem(userinfo.m_avatar, userinfo.m_nickName);
    layout->addWidget(currentUser, 0, 1);

    QPushButton* deleteFriendBtn = new QPushButton("删除好友");
    deleteFriendBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90, 90, 90); border-radius: 5px; }"
                                   "QPushButton:pressed { background-color: rgb(235, 235, 235); }");
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);

    // 绑定槽函数
    // 新增点击添加按钮，弹出 选择好友窗口
    connect(addItem->getAvatarBtn(), &QPushButton::clicked, this, [=]{
        ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog();
        connect(chooseFriendDialog, &ChooseFriendDialog::accepted, this, &SessionDetailWidget::close);
        chooseFriendDialog->exec();
    });
    // 删除好友 调用已有函数
    connect(deleteFriendBtn, &QPushButton::clicked, this, [=]{
        // 1. 弹出对话框, 确认是否要删除
        auto result = QMessageBox::warning(this, "确认删除", "确认删除该好友?", QMessageBox::Ok | QMessageBox::Cancel);
        if (result != QMessageBox::Ok) {
            LOG() << "删除好友取消";
            return;
        }

        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        dataCenter->deleteFriendAsync(userinfo.m_userId);
        this->close();
    });
}
